package program;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.block.LineBorder;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import stock.Company;

public class CompanyInformationGUI {

	public static void run(Company company, Company merval){
		
		final JFrame frame = new JFrame(company.getName());
		frame.setSize(new Dimension(GUIConstants.frameWidth,GUIConstants.frameHeight));
		frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		frame.setLocationRelativeTo(null);
		
		JPanel informationPanel = createCompanyInformationPanel(company,merval);
		
		ChartPanel chartPanel = createChartPanel(company);
		
		//Container Panel
		JPanel containerPanel = new JPanel();
		containerPanel.setLayout(new BoxLayout(containerPanel, BoxLayout.X_AXIS));
		containerPanel.setBackground(new Color(173,239,176));
		
		informationPanel.setBackground(new Color(173,239,176));
		
		
		containerPanel.add(Box.createRigidArea(new Dimension(5,0)));
		containerPanel.add(informationPanel);
		containerPanel.add(Box.createRigidArea(new Dimension(5,0)));
		containerPanel.add(chartPanel);
		containerPanel.add(Box.createRigidArea(new Dimension(5,0)));

		frame.getContentPane().add(containerPanel);
		frame.setVisible(true);
	}

	private static JPanel createCompanyInformationPanel(Company company, Company merval) {

		//Company Info
		JLabel companyInfo = new JLabel("COMPANY INFORMATION");
		companyInfo.setFont(GUIConstants.fontBold);
		companyInfo.setForeground(GUIConstants.blackColor);
		JLabel volume = new JLabel("Total Volume: "+company.getVolume());
		volume.setFont(GUIConstants.fontNormal);
		volume.setForeground(GUIConstants.companyNameColor);
		JLabel companyLast = new JLabel("Last: "+company.getLast());
		companyLast.setFont(GUIConstants.fontNormal);
		companyLast.setForeground(GUIConstants.companyNameColor);
		JLabel mktCapitalization = new JLabel("Market Capitalization: "+(company.getLast()*company.getVolume()));
		mktCapitalization.setFont(GUIConstants.fontNormal);
		mktCapitalization.setForeground(GUIConstants.companyNameColor);
		JLabel variation = new JLabel("Variation: "+company.getVariation()+" ["+company.getPorcentualVariation()+"%]");
		variation.setFont(GUIConstants.fontNormal);
		variation.setForeground(GUIConstants.companyNameColor);
		JLabel weekPercVariation = new JLabel("Week Percentage Variation: "+((company.getWeekInfo().get(4)/company.getWeekInfo().get(0))-1)*100+"%");
		weekPercVariation.setFont(GUIConstants.fontNormal);
		weekPercVariation.setForeground(GUIConstants.companyNameColor);
		
		//Merval Panel
		JPanel mervalPanel = createMervalInformationPanel(merval);
		
		//Information Panel
		JPanel informationPanel = new JPanel();
		informationPanel.setLayout(new BoxLayout(informationPanel, BoxLayout.Y_AXIS));
		informationPanel.setAlignmentY(Component.TOP_ALIGNMENT);
		informationPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.black,2),BorderFactory.createEmptyBorder(10,10,10,10)));
		informationPanel.setBackground(GUIConstants.whiteColor);
		
		informationPanel.add(companyInfo);
		informationPanel.add(volume);
		informationPanel.add(companyLast);
		informationPanel.add(mktCapitalization);
		informationPanel.add(variation);
		informationPanel.add(weekPercVariation);
		
		JPanel completeInformationPanel = new JPanel();
		completeInformationPanel.setLayout(new BoxLayout(completeInformationPanel, BoxLayout.Y_AXIS));
		completeInformationPanel.setAlignmentY(Component.TOP_ALIGNMENT);
		
		completeInformationPanel.add(informationPanel);
		completeInformationPanel.add(Box.createRigidArea(new Dimension(0,70)));
		completeInformationPanel.add(mervalPanel);
		
		return completeInformationPanel;
	}
	
	private static ChartPanel createChartPanel(Company company) {
		//Chart Panel
		XYSeriesCollection dataset = new XYSeriesCollection();
		XYSeries series = new XYSeries(company.getName());
		double i = 1.0;
		for (Float number : company.getWeekInfo()){
			series.add(i,Double.parseDouble(number+""));
			i++;
		}
		dataset.addSeries(series);	
		
		JFreeChart chart = ChartFactory.createXYLineChart("Week Fluctuation", "X", "Y", dataset, PlotOrientation.VERTICAL, true, false, false);
		ChartPanel chartPanel = new ChartPanel(chart);
		chartPanel.setBorder(BorderFactory.createLineBorder(Color.black,2));
		
		return chartPanel;
	}
	
	private static JPanel createMervalInformationPanel(Company company) {
		//Merval Panel
		JLabel mervalIndicator = new JLabel("MERVAL INDICATOR");
		mervalIndicator.setFont(GUIConstants.fontBold);
		mervalIndicator.setForeground(GUIConstants.blackColor);
		
		JLabel last = new JLabel("Last: "+company.getLast());
		last.setFont(GUIConstants.fontNormal);
		last.setForeground(GUIConstants.companyNameColor);
		
		JLabel variation = new JLabel("Variation: "+company.getVariation());
		variation.setFont(GUIConstants.fontNormal);
		variation.setForeground(GUIConstants.companyNameColor);
		
		String mktType = (company.getVariation()>0) ? "Bull Market" : "Bear Market";
		JLabel marketType = new JLabel("Market Type: "+mktType);
		marketType.setFont(GUIConstants.fontNormal);
		marketType.setForeground(GUIConstants.companyNameColor);
		
		JPanel mervalPanel = new JPanel();
		mervalPanel.setLayout(new BoxLayout(mervalPanel, BoxLayout.Y_AXIS));
		mervalPanel.setAlignmentY(Component.TOP_ALIGNMENT);
		mervalPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.black,2),BorderFactory.createEmptyBorder(10,10,10,10)));
		mervalPanel.setBackground(GUIConstants.whiteColor);
		
		mervalPanel.add(mervalIndicator);
		mervalPanel.add(last);
		mervalPanel.add(variation);
		mervalPanel.add(marketType);
		
		return mervalPanel;
	}
	
}
